package com.sephiron.clocksynchronizer;

import java.net.URI;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
import java.util.UUID;

import org.json.JSONException;
import org.json.JSONObject;

import android.net.Uri;
import android.util.Log;

public class Cctv {
	private static final String JSON_ID = "id";
	private static final String JSON_NAME = "name";
	private static final String JSON_CCTV = "cctv";
	private static final String JSON_REAL = "real";
	private static final String JSON_DESC = "description";
	private static final String TAG = "Cctv";

	private UUID mId;
	private String mCctvName;
	private Calendar mCctvCalendar, mRealCalendar;
	private String mCctvDesc;
    private ArrayList<Uri> mPhotos;

    public boolean addPhoto(Uri pathToPhoto) {
        return mPhotos.add(pathToPhoto);
    }

    public Uri getPhoto(int index) {
        return mPhotos.get(index);
    }

    public void removePhoto(int index) {
        mPhotos.remove(index);
    }

	public Cctv(String cctvName, Calendar cctvDate, Calendar realDate) {
		mId = UUID.randomUUID();
		mCctvName = cctvName;
		mCctvCalendar = cctvDate;
		mRealCalendar = realDate;
	}

	public Cctv(String cctvName, Calendar cctvDate, Calendar realDate,
			String cctvDesc) {
		mId = UUID.randomUUID();
		mCctvName = cctvName;
		mCctvCalendar = cctvDate;
		mRealCalendar = realDate;
		mCctvDesc = cctvDesc;
	}

	public Cctv() {
		mId = UUID.randomUUID();
		mCctvCalendar = Calendar.getInstance();
		mRealCalendar = Calendar.getInstance();
		mCctvName = SimpleDateFormat.getInstance().format(
				mCctvCalendar.getTime());
	}

	public Cctv(JSONObject json) throws JSONException {
		mId = UUID.fromString(json.getString(JSON_ID));
		if (json.has(JSON_NAME)) {
			mCctvName = json.getString(JSON_NAME);
		}
		mCctvCalendar = Calendar.getInstance();
		mCctvCalendar.setTimeInMillis(json.getLong(JSON_CCTV));
		
		mRealCalendar = Calendar.getInstance();
		mRealCalendar.setTimeInMillis(json.getLong(JSON_REAL));

		if (json.has(JSON_DESC)) {
			mCctvDesc = json.getString(JSON_DESC);
			Log.d(TAG,"CctvDesc got from JSON : " + mCctvDesc);
		}
	}

	public String getCctvName() {
		return mCctvName;
	}

	public void setCctvName(String cctvName) {
		mCctvName = cctvName;
	}

	/**
	 * 
	 * @return CCTV time Calendar
	 */
	public Calendar getCctvCalendar() {
		return mCctvCalendar;
	}

	public void setCctvCalendar(Calendar cctvDate) {
		mCctvCalendar = cctvDate;
//		Log.d(TAG, Log.getStackTraceString(new Exception()));
	}

	public void setCctvDate(long cctvDate) {
		mCctvCalendar.setTimeInMillis(cctvDate);
	}

	/**
	 * 
	 * @return Real time Calendar
	 */
	public Calendar getRealCalendar() {
		return mRealCalendar;
	}

	public void setRealCalendar(Calendar realDate) {
		mRealCalendar = realDate;
//		Log.d(TAG, Log.getStackTraceString(new Exception()));
	}

	public void setRealDate(long realDate) {
		mRealCalendar.setTimeInMillis(realDate);
	}

	public UUID getId() {
		return mId;
	}

	public JSONObject toJSON() throws JSONException {
		JSONObject json = new JSONObject();
		json.put(JSON_ID, mId.toString());
		json.put(JSON_NAME, mCctvName);
		json.put(JSON_CCTV, mCctvCalendar.getTimeInMillis());
		json.put(JSON_REAL, mRealCalendar.getTimeInMillis());
		json.put(JSON_DESC, mCctvDesc);
		Log.d(TAG,"CctvDesc put in JSON : " + mCctvDesc);

		return json;
	}

	@Override
	public String toString() {
		return mCctvName+" : "+mCctvDesc;
	}

	public String getCctvDesc() {
		return mCctvDesc;
	}

	public void setCctvDesc(String cctvDesc) {
		mCctvDesc = cctvDesc;
		Log.d(TAG,"CctvDesc set to : " + mCctvDesc);
	}

}
